// pages/guiders/guiderdetail.js
var Tool = require("../../utils/ztool.js");
var tool = new Tool();
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IDD: '',
    detail: '',
    driverlist: [],
    telNum: '',
  },

  //推送方法
  PushTo: function (status) {
    var that = this
    var cont = ''
    var cont2 = ''
    if (status == '0') {
      cont = '导游接受了您的订单'
      cont2 = '订单已接受'
    }
    if (status == '3') {
      cont = '导游拒绝了您的订单'
      cont2 = '订单已拒绝'
    }
    wx.showToast({
      title: '正在加载...',
      icon: 'loading',
      duration: 10000
    });
    console.log(that.data.telNum)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/Weixin/WxPushHdl.ashx',
      data: {
        idd: that.data.IDD,
        tel: that.data.telNum,
        title: cont,
        content: cont2,
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        // var data = tool.getRegularJson(res.data);
        var code = res.data.code;
        console.log(res);
        if (code == 0) {
          console.log('post推送请求成功')

        } else {
          console.log(res)
        }
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function () {
        // complete
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    })
  },

  showError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },

  locateDriver: function (e) {
    var that = this
    var dateEnd = e.currentTarget.dataset.idx
    var isopen = e.currentTarget.dataset.open
    console.log(dateEnd)
    var time1 = util.formatTime1(new Date())
    console.log(time1)
    if (dateEnd < time1) {
      that.showError('订单已过期')
      return
    }
    if (isopen == 0) {
      that.showError('该司机尚未开启定位功能')
      return
    }

    var driverID = e.currentTarget.id
    wx.navigateTo({
      url: '../travelAgency/locate?IDD=' + that.data.IDD + '&DriverID=' + driverID + '&retype=3',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      IDD: options.IDD
    });
    console.log(this.data.IDD)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getServiceData()
    this.getDriverandCarData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getServiceData: function () {
    var that = this
    that.data.telNum = ''
    var useinfo = wx.getStorageSync('userInfo')
    var tel = useinfo.sTel

    wx.request({
      url: 'https://www.lanshuicar.com/Server/Guiders/OrderInfo.ashx',
      data: {
        IDD: that.data.IDD,
        methd: 0,
        Tel: tel
      },
      method: 'GET',
      success: function (res) {
        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.data.length > 0) {
          that.setData({
            detail: data.data[0],
          });

          var starttime = that.data.detail.UseCarStartTime
          var endtime = that.data.detail.UseCarEndTime

          if (starttime.substring(11, 16) == '00:00' && endtime.substring(11, 16) == '23:59') {
            that.setData({
              UseCarStart: starttime.substring(0, 10),
              UseCarEnd: endtime.substring(0, 10)
            })
          } else {
            that.setData({
              UseCarStart: starttime.substring(0, 16),
              UseCarEnd: endtime.substring(0, 16)
            })
          }

          var arr = that.data.detail
          that.data.telNum = arr.AgencyTel
          console.log(that.data.telNum)
        }
      },
      fail: function () {
        console.log('加载错误');
        that.setData({
          loadError: true
        });

      }
    })
  },

  getDriverandCarData: function () {
    var that = this
    that.setData({
      driverlist: [],
    });
    console.log(that.data.driverlist.length)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/DriverPerson/GetDriverCarInfo.ashx',
      data: {
        IDD: that.data.IDD,
      },
      method: 'GET',
      success: function (res) {
        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.data.length > 0) {
          that.setData({
            driverlist: that.data.driverlist.concat(data.data),
          });

          console.log(that.data.driverlist)
        }
      },
      fail: function () {
        console.log('加载错误');
        wx.hideToast();
        that.setData({
          loadError: true
        });
      }
    })
  },

  agreeorder: function () {
    var that = this
    var useinfo = wx.getStorageSync('userInfo')
    console.log(useinfo)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/RefuseorReOrder.ashx',
      data: {
        IDD: that.data.IDD,
        ID: useinfo.Id,
        status: 0,
        Type: useinfo.USerType,
      },
      method: 'GET',
      success: function (res) {
        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.code == 0) {
          wx.showToast({
            title: '接受订单',
            icon: 'success',
            duration: 2000
          })
          that.PushTo('0')
          that.getServiceData()
          that.getDriverandCarData()

          var pages = getCurrentPages()
          var backpage = pages[pages.length - 2]
          backpage.setData({
            backtag: 1,
          });
        }
      },
      fail: function () {
        console.log('加载错误');
        wx.hideToast();
        that.setData({
          loadError: true
        });
      }
    })
  },

  cancelorder: function () {
    var that = this
    var useinfo = wx.getStorageSync('userInfo')
    console.log(useinfo)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/RefuseorReOrder.ashx',
      data: {
        IDD: that.data.IDD,
        ID: useinfo.Id,
        status: 3,
        Type: useinfo.USerType,
      },
      method: 'GET',
      success: function (res) {
        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.code == 0) {
          wx.showToast({
            title: '拒绝订单',
            icon: 'success',
            duration: 2000
          })
          that.PushTo('3')
          that.getServiceData()
          that.getDriverandCarData()

          var pages = getCurrentPages()
          var backpage = pages[pages.length - 2]
          backpage.setData({
            backtag: 1,
          });
        }
      },
      fail: function () {
        console.log('加载错误');
        wx.hideToast();
        that.setData({
          loadError: true
        });
      }
    })
  },

  calldriver: function (e) {
    console.log(e)
    var phoneNumber = e.currentTarget.dataset.text
    console.log(phoneNumber)
    if (phoneNumber == "") {
      wx.showToast({
        title: '号码为空',
        icon: 'none',
      });
      return;
    }
    wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },

  seetourist: function () {
    var that = this
    wx.navigateTo({
      url: '/pages/guiders/seetourist/seetourist?IDD=' + that.data.IDD + '&endtime=' + that.data.detail.UseCarEndTime
    })
  }
})